<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-form label-width="80px" size="small" v-formkey="{query:toQuery}">
            <el-row :gutter="10">
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="编码">
                  <el-input v-model="query.code" placeholder="输入编码搜索"/>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="名称">
                  <el-input v-model="query.name" placeholder="输入名称搜索"/>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="仓库类型">
                  <el-select v-model="query.type" class="edit-input" placeholder="请选择" style="width: 100%;">
                    <el-option
                        v-for="(item,index) in dict.t_bas_warehouse_type"
                        :key="index"
                        v-if="item.seq>0"
                        :label="item.label"
                        :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

            </el-row>
          </el-form>
          <rrOperation :crud="crud"/>
        </div>

        <crudOperation :permission="permission">
        </crudOperation>
      </div>

      <div class="body-container">
        <!--表单组件-->
        <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                   :title="crud.status.title" width="800px">
          <el-form ref="form" :model="form" :rules="rules" size="small" label-width="180px">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" style="width: 370px;"/>
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" style="width: 370px;"/>
            </el-form-item>
            <el-form-item label="仓库类型">
              <el-select v-model="form.type" class="edit-input" placeholder="请选择" style="width: 370px;" @change="changeType">
                <el-option
                    v-for="(item,index) in dict.t_bas_warehouse_type"
                    :key="index"
                    v-if="item.seq>0"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="是否批次管理">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isBatchManage"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否库位管理">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isStorehouseManage"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否发货仓库">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isSend"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否退货仓库">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isReturn"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否允许负库存">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isAllowNegativeStock"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否允许超可用量发货">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isAllowOverUsableSend"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否开放可用量">
                  <!--            <el-input v-model="form.isBuy" style="width: 370px;"/>-->
                  <el-switch
                      v-model="form.isOpenUsableNum"
                      active-color="#13ce66"
                      :active-value="1"
                      :inactive-value="0"
                      inactive-color="#ff4949">
                  </el-switch>
                </el-form-item>
              </el-col>
            </el-row>


            <!--          <el-form-item label="部门id">-->
            <!--            <el-input v-model="form.deptId" style="width: 370px;" />-->
            <!--          </el-form-item>-->
            <!--          <el-form-item label="部门编码">-->
            <!--            <el-input v-model="form.deptCode" style="width: 370px;" />-->
            <!--          </el-form-item>-->
            <!--          <el-form-item label="部门名称">-->
            <!--            <el-input v-model="form.deptName" style="width: 370px;" />-->
            <!--          </el-form-item>-->
            <el-form-item label="仓库负责人">
              <zSelect :multiple="multiple" :value.sync="form.userIds" :showName="form.userName" :label="form.userName" url="api/dropDown/sysUserList" :params="{businessAttr:'8'}"
                       style="width:  370px;"/>
            </el-form-item>
            <el-form-item label="发货优先级">
              <el-input-number v-model="form.sendSeq" :min="1" style="width: 370px;"/>
            </el-form-item>
            <el-form-item label="退货优先级">
              <el-input-number v-model="form.returnSeq" :min="1" style="width: 370px;"/>
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="form.address" style="width: 370px;"/>
            </el-form-item>
            <el-form-item label="经纬度">
              <el-input v-model="form.location" style="width: 370px;"/>
            </el-form-item>
            <el-form-item label="排序">
              <el-input-number v-model="form.seq" :min="1" label="排序" style="width: 370px;"></el-input-number>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.status" class="edit-input" placeholder="请选择" style="width: 370px;">
                <el-option
                    v-for="(item,index) in dict.common_status"
                    :key="index"
                    v-if="item.seq>0"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remark" style="width: 370px;" type="textarea"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="crud.cancelCU">取消</el-button>
            <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
          </div>
        </el-dialog>

        <vue-context-menu :contextMenuData="contextMenuData"
                          @tableConfig="tableConfig"
                          @deletedata="deletedata"></vue-context-menu>
        <!-- 表格配置 -->
        <el-dialog title="表格配置"
                   :visible.sync="tableConfigShow"
                   width="60%">
          <j-table-column-config :columns.sync="crud.header"  @tabSave="tabSave" @pit="pit" />
        </el-dialog>

        <!--表格渲染-->
        <el-table ref="table"  border
                  height="700"
                  show-summary
                  @cell-dblclick="row=>crud.toEdit(row)"
                  v-loading="crud.loading"
                  :data="crud.data"
                  size="small"
                  style="width: 100%;"
                  v-if="isShowTable"
                  @header-dragend="changeColWidth"
                  @contextmenu.native="showMenu"
                  :summary-method="crud.sumRow"
                  @row-click="crud.selectRow"
                  :row-style="crud.rowClass"
                  :row-class-name="crud.tableRowClassName"
                  @selection-change="crud.selectionChangeHandler">
          <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
          <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
          <el-table-column
            v-if="item.visible"
            v-for="(item, index) in crud.header"
            :key="`col_${index}`"
            :prop="item.prop"
            :label="columnLabel(item)"
            :width="item.width + 'px'"
            :data-porp='item.prop'
            @contextmenu.native="showMenu"
            @header-dragend="changeColWidth"
          >
            <!-- 自定义渲染模版 -->
            <template slot-scope="scope">
              <!-- 当列key为 sort 时，渲染一个手柄icon -->
              <template v-if="item.prop =='sort'">
                <div class="handle_move">
                  <i :class="scope.row['sort']" />
                </div>
              </template>
              <template v-else>
                <span v-if="item.type == 1" style="margin-left: 0px">{{scope.row[`${item.prop}`]}}</span>
                <span v-if="item.type == 2" style="margin-left: 0px">{{ parseTime(scope.row[`${item.prop}`])}}</span>
                <span v-if="item.type == 3" style="margin-left: 0px">{{ getDict(`this.dict.label.${item.dictionary}["${scope.row[item.prop]}"]`) }}</span>
              </template>
            </template>
          </el-table-column>
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" prop="code" label="编码"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('name')" prop="name" label="名称"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('type')" prop="type" label="仓库类型">-->
<!--            <template slot-scope="scope">-->
<!--              {{ dict.label.t_bas_warehouse_type[scope.row.type] }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isBatchManage')" prop="isBatchManage" label="是否批次管理">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isBatchManage == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isStorehouseManage')" prop="isStorehouseManage" label="是否库位管理">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isStorehouseManage == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isSend')" prop="isSend" label="是否发货仓库">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isSend == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isReturn')" prop="isReturn" label="是否退货仓库">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isReturn == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isAllowNegativeStock')" prop="isAllowNegativeStock" label="是否允许负库存">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isAllowNegativeStock == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isAllowOverUsableSend')" prop="isAllowOverUsableSend"-->
<!--                           label="是否允许超可用量发货">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isAllowOverUsableSend == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isOpenUsableNum')" prop="isOpenUsableNum" label="是否开放可用量">-->
<!--            <template slot-scope="scope">-->
<!--              {{ scope.row.isOpenUsableNum == 1 ? '是' : '否' }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptId')" prop="deptId" label="部门id" />&ndash;&gt;-->
<!--          &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptCode')" prop="deptCode" label="部门编码" />&ndash;&gt;-->
<!--          &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptName')" prop="deptName" label="部门名称" />&ndash;&gt;-->
<!--          &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userId')" prop="userId" label="人员id" />&ndash;&gt;-->
<!--          &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userCode')" prop="userCode" label="人员编码" />&ndash;&gt;-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userName')" prop="userName" label="仓库负责人"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sendSeq')" prop="sendSeq" label="发货优先级"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('returnSeq')" prop="returnSeq" label="退货优先级"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('address')" prop="address" label="地址"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" prop="remark" label="备注"/>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('status')" label="状态" prop="status">-->
<!--            <template slot-scope="scope">-->
<!--              {{ dict.label.common_status[scope.row.status] }}-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column :render-header="crud.renderHeader" v-permission="['admin','basWarehouse:edit','basWarehouse:del']" label="操作" width="150px"-->
<!--                           align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <udOperation-->
<!--                  :data="scope.row"-->
<!--                  :permission="permission"-->
<!--              />-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>
        <!--分页组件-->
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudBasWarehouse from '@/api/erp/basWarehouse'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {searchUser} from "@/api/system/user";

import { publicListKey } from "@/utils/key";
import {getHeaderProperties} from "@/api/erp/headerProperties";
import {tablemixins} from "@/mixins/tablemixins";
import  jTableColumnConfig  from "@/components/jTableColumnConfig/jTableColumnConfig";

// crud交由presenter持有
const defaultCrud = CRUD({title: '仓库', url: 'api/basWarehouse', sort: 'code,asc', crudMethod: {...crudBasWarehouse}})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  isBatchManage: null,
  isStorehouseManage: null,
  isSend: null,
  isReturn: null,
  isAllowNegativeStock: null,
  isAllowOverUsableSend: null,
  isOpenUsableNum: null,
  deptId: null,
  deptCode: null,
  deptName: null,
  userIds: null,
  userCode: null,
  userName: null,
  sendSeq: null,
  returnSeq: null,
  address: null,
  seq: null,
  status: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'BasWarehouse',
  components: {pagination, crudOperation, rrOperation, udOperation,jTableColumnConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts: ['t_bas_warehouse_type', 'common_status','common_yes_or_no'],
  data() {
    return {
      multiple:true,
      permission: {
        add: ['admin', 'basWarehouse:add'],
        edit: ['admin', 'basWarehouse:edit'],
        del: ['admin', 'basWarehouse:del'],
        import: ['admin', 'basWarehouse:import'],
        export: ['admin', 'basWarehouse:export'],
        copy: ['admin', 'basWarehouse:copy'],
        upload: ['admin', 'basWarehouse:upload'],
        download: ['admin', 'basWarehouse:download'],
        enable: ['admin', 'basWarehouse:enable'],
        disable: ['admin', 'basWarehouse:disable'],
      },
      loading: false,
      rules: {
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '编码不能为空', trigger: 'blur'}
        ],
      },
      userList: []
    }
  },
  watch: {},
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    //远程搜索用户
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        searchUser({keyword: query}).then(res => {
          this.loading = false
          this.userList = res.data.content;
          this.$forceUpdate()
        })
      } else {
        this.userList = [];
      }
    },
    changeType(e){
      console.log(e);
      if(!e){
        return false;
      }
      this.form.isBatchManage=0 //是否批次管理
      this.form.isStorehouseManage=0 //是否库位管理
      this.form.isSend=0 //是否发货仓库
      this.form.isReturn=0 //是否退货仓库
      this.form.isAllowNegativeStock=0 //是否允许负库存
      this.form.isAllowOverUsableSend=0 //是否允许超可用量发货
      this.form.isOpenUsableNum=0 //是否开放可用量

      switch (e) {
        case 1: //发货仓
          this.form.isSend=1
          this.form.isOpenUsableNum=1
          break;
        case 2: //备货仓
          this.form.isOpenUsableNum=1
          break;
      }

    }
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
